/*******************************************************************************
 * Copyright (c) 2011 Olexandr V. Shaposhnikov.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Olexandr V. Shaposhnikov - initial API and implementation and/or initial documentation
 *******************************************************************************/
package com.funkyroach.msvctch.msvc2008;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

import com.funkyroach.msvctch.plugin.MSVCPlugin;

public class MSVC2008WorkbenchPreferencePage extends PreferencePage implements
		IWorkbenchPreferencePage {

	private Text msvcHomeText;
	@SuppressWarnings("unused")
	private Control external;
	private Text msSDKHomeText;
	private Text msDXSDKHomeText;

	@Override
	protected Control createContents(Composite parent) {
		Composite composite = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		layout.marginWidth = 0;
		layout.marginHeight = 0;
		layout.numColumns = 1;
		composite.setLayout(layout);
		composite.setLayoutData(new GridData());
		external = createExternalArea(composite);

		Dialog.applyDialogFont(parent);
		return composite;
	}

	protected Control createExternalArea(Composite parent) {

		Composite composite = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		layout.marginWidth = 5;
		layout.marginHeight = 5;
		layout.numColumns = 3;
		composite.setLayout(layout);
		composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		createMSVCHome(composite);
		createMSSDKHome(composite);
		createDXSDKHome(composite);

		initializeDefaults();
		return composite;
	}

	private void createMSVCHome(Composite composite) {

		Label intro = new Label(composite, SWT.LEFT | SWT.WRAP);
		intro.setText("Microsoft Visual Studio 9.0 VC Home");
		GridData data = new GridData();
		data.horizontalSpan = 3;
		data.horizontalAlignment = GridData.FILL;
		data.widthHint = 300;
		intro.setLayoutData(data);

		new Label(composite, SWT.LEFT).setText("");
		msvcHomeText = new Text(composite, SWT.BORDER);
		msvcHomeText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		final Button b = new Button(composite, SWT.NONE);
		b.setText("Browse");
		data = new GridData();
		data.horizontalAlignment = GridData.FILL;
		int widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH);
		data.widthHint = Math.max(widthHint, b.computeSize(SWT.DEFAULT,
				SWT.DEFAULT, true).x);
		b.setLayoutData(data);
		b.addListener(SWT.MouseDown, new Listener() {
			public void handleEvent(Event event) {
				DirectoryDialog d = new DirectoryDialog(getShell());
				d.setText(msvcHomeText.getText());
				String file = d.open();
				if (file != null) {
					setMSVC_2008_HOME(file);
				}
			}
		});
	}

	private void createMSSDKHome(Composite composite) {
		Label intro = new Label(composite, SWT.LEFT | SWT.WRAP);

		intro.setText("Microsoft SDK");
		GridData data = new GridData();
		data.horizontalSpan = 3;
		data.horizontalAlignment = GridData.FILL;
		data.widthHint = 300;
		intro.setLayoutData(data);

		new Label(composite, SWT.LEFT).setText("");
		msSDKHomeText = new Text(composite, SWT.BORDER);
		msSDKHomeText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		final Button b = new Button(composite, SWT.NONE);
		b.setText("Browse");
		data = new GridData();
		data.horizontalAlignment = GridData.FILL;
		int widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH);
		data.widthHint = Math.max(widthHint, b.computeSize(SWT.DEFAULT,
				SWT.DEFAULT, true).x);
		b.setLayoutData(data);
		b.addListener(SWT.MouseDown, new Listener() {
			public void handleEvent(Event event) {
				DirectoryDialog d = new DirectoryDialog(getShell());
				d.setText(msSDKHomeText.getText());
				String file = d.open();
				if (file != null) {
					setMSSDK_HOME(file);
				}
			}
		});
	}

	
	private void createDXSDKHome(Composite composite) {
		Label intro = new Label(composite, SWT.LEFT | SWT.WRAP);

		intro.setText("Microsoft DirectX SDK");
		GridData data = new GridData();
		data.horizontalSpan = 3;
		data.horizontalAlignment = GridData.FILL;
		data.widthHint = 300;
		intro.setLayoutData(data);

		new Label(composite, SWT.LEFT).setText("");
		msDXSDKHomeText = new Text(composite, SWT.BORDER);
		msDXSDKHomeText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		final Button b = new Button(composite, SWT.NONE);
		b.setText("Browse");
		data = new GridData();
		data.horizontalAlignment = GridData.FILL;
		int widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH);
		data.widthHint = Math.max(widthHint, b.computeSize(SWT.DEFAULT,
				SWT.DEFAULT, true).x);
		b.setLayoutData(data);
		b.addListener(SWT.MouseDown, new Listener() {
			public void handleEvent(Event event) {
				DirectoryDialog d = new DirectoryDialog(getShell());
				d.setText(msDXSDKHomeText.getText());
				String file = d.open();
				if (file != null) {
					setDXSDK_HOME(file);
				}
			}
		});
	}


	protected IPreferenceStore doGetPreferenceStore() {
		return MSVCPlugin.getDefault().getPreferenceStore();
	}

	protected void performDefaults() {
		initializeDefaults();
		super.performDefaults();
	}

	public boolean performOk() {
		MSVC2008PathResolver.checked = false;
		MSVC2008PathResolver.setMsvcHome(msvcHomeText.getText());
		MSVC2008PathResolver.setMsSDKHome(msSDKHomeText.getText());
		MSVC2008PathResolver.setDXSDKHome(msDXSDKHomeText.getText());
		
		return super.performOk();
	}
	
	

	private void initializeDefaults() {
		msvcHomeText.setText(MSVC2008PathResolver.getMsvcHome());
		msSDKHomeText.setText(MSVC2008PathResolver.getMsSDKHome());
		msDXSDKHomeText.setText(MSVC2008PathResolver.getDXSDKHome());
	}

	protected void setMSVC_2008_HOME(String s) {
		msvcHomeText.setText(s);
	}

	protected void setMSSDK_HOME(String s) {
		msSDKHomeText.setText(s);
	}

	protected void setDXSDK_HOME(String s) {
		msDXSDKHomeText.setText(s);
		
	}

	public void init(IWorkbench workbench) {
		// TODO Auto-generated method stub

	}

}
